package com.java.zhuiyun.util;

import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;


/**
 * @ClassName EmailUtils
 * @Description TODO
 * @Author MJL
 * @Date 2023/8/29 9:19
 **/
@Component
public class MailUtil {

    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送简单的邮箱
     * @param to 收件人
     * @param theme 标题
     * @param content 正文内容
     * @param cc 抄送
     */
    public void sendSimpleMail(String to, String theme, String content, String... cc) {
        // 创建邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 发件人
        try {
            message.setFrom(String.valueOf(new InternetAddress(from, "智学IT课堂", "UTF-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 收件人
        message.setTo(to);
        // 标题
        message.setSubject(theme);
        // 内容
        message.setText(content);
        // 判断是否有抄送人
        if (ArrayUtils.isNotEmpty(cc)) {
            message.setCc(cc);
        }
        // 发送
        javaMailSender.send(message);
    }
}
